package com.developer.fans;

import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.coocaa.x.framework.utils.Android;
import com.coocaa.x.framework.utils.ThirdMac;
import com.skyworth.framework.skysdk.properties.SkySystemProperties;
import com.skyworth.webSDK.factory.XWebComplexFactory;
import com.skyworth.webSDK.webservice.XRestClient;
import com.skyworth.webSDK.webservice.tcappstore.AppStoreServiceImpl;

/**
 * Created by lu on 15-8-11.
 */
public class CrackApplication extends Application {
    private static final String APPSTORE_PKGNAME = "com.tianci.appstore";
    private static Context mContext = null;

    public static Context getContext() {
        return mContext;
    }


    private static final Handler handler = new Handler(Looper.getMainLooper());

    public static void runOnUiThread(Runnable run) {
        handler.post(run);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
    }

    private static AppStoreServiceImpl appstore = null;

    private static void initAppStoreWebService() {
        try {
            AppStoreInfo info = getAppStoreInfo();
            String server = info.server;
            String channel = info.content_channel;
            String deviceID = getMAC();
            String session = "00dd5ca1626de5733c0a06932834a098-forold";
            Log.d("test", "create  " + channel + " " + deviceID + " " + session + " " + server);
            XRestClient.setSdkver(String.valueOf(android.os.Build.VERSION.SDK_INT));
            XRestClient.setGamecenter_channel("SkyGames");
            XRestClient.setChannel(channel);
            XRestClient.setTvinfo(deviceID);
            XRestClient.setSession(session);

            appstore = (AppStoreServiceImpl) XWebComplexFactory.getInstance(server).getClassInstance(
                    AppStoreServiceImpl.class.getName());
            appstore.setHttpMethod("POST");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static synchronized AppStoreServiceImpl getAppStoreService() {
        if (appstore == null)
            initAppStoreWebService();
        return appstore;
    }

    public static class AppStoreInfo {
        public long versionCode = 0;
        public String versionName = null;

        public String device_channel = null;
        public String content_channel = null;
        public String server = null;
    }

    public static class AppStoreNotInstalledException extends Exception {
        private static final String ERROR = "未检测到应用圈";

        public AppStoreNotInstalledException() {
            super(ERROR);
        }
    }

    private static AppStoreInfo mAppStoreInfo = null;

    public static synchronized AppStoreInfo getAppStoreInfo() throws Exception {
        if (mAppStoreInfo == null) {
            if (!Android.isPackageInstalled(getContext(), APPSTORE_PKGNAME))
                throw new AppStoreNotInstalledException();
            mAppStoreInfo = new AppStoreInfo();
            mAppStoreInfo.versionCode = Android.getVersionCode(getContext(), APPSTORE_PKGNAME);
            mAppStoreInfo.versionName = Android.getVersionName(getContext(), APPSTORE_PKGNAME);
            mAppStoreInfo.server = (String) Android.getMetaData(getContext(), APPSTORE_PKGNAME, "SERVER");
            mAppStoreInfo.device_channel = (String) Android.getMetaData(getContext(), APPSTORE_PKGNAME, "UMENG_CHANNEL");
            try {
                mAppStoreInfo.content_channel = (String) Android.getMetaData(getContext(), APPSTORE_PKGNAME, "CONTENT_CHANNEL");
            } catch (Exception e) {
                mAppStoreInfo.content_channel = mAppStoreInfo.device_channel;
            }
        }
        return mAppStoreInfo;
    }


    private static String getMAC() {
        Context context = getContext();
        String mac = SkySystemProperties.getProperty("third.get.mac");
        if (mac != null && !mac.equals(""))
            return mac;
        mac = ThirdMac.getEtherMac();
        if (mac != null && !mac.equals(""))
            return mac;
        mac = ThirdMac.getWifiMac();
        if (mac != null && !mac.equals(""))
            return mac;
        android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        mac = wifi.getConnectionInfo().getMacAddress();
        if (mac != null && !mac.equals(""))
            return mac;
        if (mac == null)
            return "";
        return mac;
    }
}